// 1. 引入这两个函数来初始化路由
import { createRouter, createWebHistory } from "vue-router";
// import { ElMessage } from "element-plus";

// 2. 配置路由
export const constantRoutes = [
  {
    path: "/",
    component: () => import("@/components/pc/sindex/sindex.vue"),
  },
  {
    path: "/Sindex",
    component: () => import("@/components/pc/sindex/sindex.vue"),
    name: "sindex",
    meta: {
      title: "首页",
      index: 0,
    },
  },
  {
    path: "/workingCondition",
    component: () => import("@/components/pc/workingCondition/workingCondition.vue"),
    name: "workingCondition",
    meta: {
      title: "工作状态",
      index: 0,
    },
  },
  {
    path: "/workingDetail",
    component: () => import("@/components/pc/workingCondition/workingDetail.vue"),
    name: "workingDetail",
    meta: {
      title: "工作状态详情",
      index: 0,
    },
  },
  {
    path: "/DataDirectory",
    component: () => import("@/components/pc/DataDirectory/DataDirectory.vue"),
    name: "DataDirectory",
    meta: {
      title: "数据目录",
      index: 0,
      needLogin: true,
    },
  },
  {
    path: "/AppListPage",
    component: () => import("@/components/pc/AppListPage/AppListPage.vue"),
    name: "AppListPage",
    meta: {
      title: "APP应用",
      index: 0,
      needLogin: true,
    },
  },
  {
    path: "/Interaction",
    component: () => import("@/components/pc/Interaction/Interaction.vue"),
    name: "Interaction",
    meta: {
      title: "互动交流",
      index: 0,
      needLogin: true,
    },
  },
  {
    path: "/OpenStatistics",
    component: () => import("@/components/pc/OpenStatistics/OpenStatistics.vue"),
    name: "OpenStatistics",
    meta: {
      title: "开发统计",
      index: 0,
      needLogin: true,
    },
  },
  {
    path: "/My",
    component: () => import("@/components/pc/My/My.vue"),
    name: "My",
    meta: {
      title: "开发统计",
      index: 0,
      needLogin: true,
    },
  },
  {
    path: "/serch",
    component: () => import("@/components/pc/serch/serch.vue"),
    name: "serch",
    meta: {
      title: "搜索",
      index: 0,
      needLogin: true,
    },
  },
  {
    path: "/demo01",
    component: () => import("@/components/pc/demo01/demo01.vue"),
    name: "demo01",
    meta: {
      title: "搜索",
      index: 0,
      needLogin: true,
    },
  },
  {
    path: "/:catchAll(.*)",
    name: "/404",
    component: () => import("@/components/pc/NoPages/NoPages.vue"),
  },
];
// 3. 创建路由实例
export const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes,
});

// 4. 你还可以监听路由拦截，比如权限验证。
router.beforeEach((to, from, next) => {
  const token = su.cook.getcookie("token");

  // 如果是首页 或者 没有登录限制的页面都可以进入
  if (to.name == "sindex" || !to.meta.needLogin) {
    su.scroll.scrollTop();
    next();
  } else {
    if (token && token != "") {
      su.scroll.scrollTop();
      next();
    } else {
      ElMessage.warning("请登录!");
    }
  }
  document.title = "数据开放平台";

  //判断是否进入的login页（需有单独的登录页）
  // if (to.name == "login") {
  //   if (token) {
  //     next("/home");
  //   } else {
  //     next();
  //   }
  // } else {
  //   if (token) {
  //     next();
  //   } else {
  //     next("/login");
  //   }
  // }
  // document.title = to.meta.title || "这里写项目名";
});

// 5.动态路由，大概率较长时间用户都不会点击的页面
// router.addRoute({
//   path: "/about",
//   component: () => import("@/views/about/index.vue"),
//   meta: {
//     title: "关于",
//   },
// });

// 6.导出
export default router;
